package cn.itcast.bos.service.base.impl;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by xbqco on 2018/7/16.
 */
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService{

    @Autowired
    private FixedAreaRepository fixedAreaRepository;

    @Autowired
    private CourierRepository courierRepository;

    @Override
    public Page<FixedArea> findAll(Pageable pageable){
        return fixedAreaRepository.findAll(pageable);
    }

    @Override
    public void save(FixedArea fixedArea) {
        fixedAreaRepository.save(fixedArea);
    }

    @Override
    public void update(FixedArea fixedArea) {

        fixedAreaRepository.save(fixedArea);
    }

    @Override
    public Page<Courier> findNoAssociationCouriers(Pageable pageable, String id) {
        FixedArea fixedArea = fixedAreaRepository.findOne(id);
        Set<Integer> ids = new HashSet<>();
        for(Courier courier :fixedArea.getCouriers()){
            ids.add(courier.getId());
        }
        return courierRepository.findByIdNotInOrderById(pageable,ids);
    }

    @Override
    public Page<Courier> findFixedAreaCouriers(Pageable pageable, String id) {
        FixedArea fixedArea = fixedAreaRepository.findOne(id);

        return courierRepository.findByFixedAreasContainingOrderById(pageable,fixedArea);
    }

    @Override
    public void associationCourierToFixedArea(String id, Integer[] courierIds) {
        System.out.println("-------------------------"+id);
        FixedArea fixedArea = fixedAreaRepository.findOne(id);
        System.out.println("数据是否传进来了:"+courierIds.length);
        for(Integer courierId:courierIds){
            Courier courier=courierRepository.findOne(courierId);
            System.out.println(courier);
            fixedArea.getCouriers().add(courier);
        }
    }

    @Override
    public void cancelAssociationCourierToFixedArea(String id, Integer[] courierIds) {
        FixedArea fixedArea = fixedAreaRepository.findOne(id);
        System.out.println("------------------------------"+fixedArea.getCouriers().size());
        if(fixedArea!=null){
            System.out.println("*****************前"+fixedArea.getCouriers().size());
        for(Integer courierId:courierIds){
            System.out.println("courierId为空"+courierId);
            Courier courier= courierRepository.findOne(courierId);

            fixedArea.getCouriers().remove(courier);

            System.out.println("走了impl");
        }}

        System.out.println("------------------------------后"+fixedArea.getCouriers().size());

    }


}
